package com.sso.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sso.entity.Maps;
import com.sso.entity.Report;
import com.sso.filter.R;
import com.sso.form.UploadDto;
import com.sso.service.MapsService;
import com.sso.service.ReportService;
import com.sso.utils.MyProperty;
import com.sso.utils.ZipUtil;
import com.sso.utils.ZipUtil2;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xia
 * @since 2022-07-23
 */

@RestController
@CrossOrigin
@Api(tags = "报表接口")
@RequestMapping("/shale_porosity/report")
public class ReportController {
    @Autowired
    private ReportService reportService;
    @Autowired
    private MapsService mapsService;
    @Autowired
    private MyProperty myProperty;
    @GetMapping("{id}")
    @PreAuthorize("hasAuthority('3')")
    @ApiOperation("通过图片id查看报表")
    public R getUser(@PathVariable Long id) {
        List<Report> reports = reportService.list(new QueryWrapper<Report>().lambda()
                .eq(Report::getPictureId, id));
        if (reports == null) {
            return R.error("报表不存在");
        }
        return R.ok().put("raw", reports);
    }

    @GetMapping("/upload")
    @ApiOperation("报表下载")
    public R upload(UploadDto uploadDto, HttpServletResponse httpServletResponse) throws IOException {
        List<Report> reports = reportService.list(new QueryWrapper<Report>().lambda()
                .eq(Report::getPictureId, uploadDto.getId())
                .eq(Report::getType,uploadDto.getType()));
        Maps one = mapsService.getOne(new LambdaQueryWrapper<Maps>().eq(Maps::getSampleId, uploadDto.getId()));
        String path=one.getPath();
        path=path.replace("data/1478130726/SE2","");
        String path2=path+"report.zip";
        ZipUtil2.compressToZip(myProperty.getPath()+reports.get(0).getResPath(),path,"report.zip");
        FileInputStream inputStream = new FileInputStream(path2);
        // 设置响应头、以附件形式打开文件
        httpServletResponse.setCharacterEncoding("UTF-8");
        String[] str = path.split("/");
        httpServletResponse.setHeader("Content-Disposition", "attachment; fileName=" + str[str.length - 1]);
        ServletOutputStream outputStream = httpServletResponse.getOutputStream();
        int len = 0;
        byte[] data = new byte[1024];
        while ((len = inputStream.read(data)) != -1) {
            outputStream.write(data, 0, len);
        }
        outputStream.close();
        inputStream.close();
        return R.ok();
    }

    @GetMapping("/{current}/{limit}")
    @ApiOperation("分页获取所有样品信息")
    public R listUsers(@PathVariable Long current, @PathVariable Long limit) {
        Page<Report> page = new Page<>(current, limit);
        QueryWrapper<Report> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        reportService.page(page, queryWrapper);
        long total = page.getTotal();
        return R.ok().put("total", total).put("raw", page.getRecords());
    }
}

